package com.classes;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by mporcino on 6/16/14.
 */
public class Library {

    private List<Item> itemsList;
    private Map<Item, User> checkedOut;

    public Library (List<Item> itemsList, Map<Item, User> checkedOut)
    {
        this.itemsList = itemsList;
        this.checkedOut = checkedOut;
    }

    public List<Item> getItemsList()
    {
        return itemsList;
    }

    public Map<Item, User> getCheckedOut()
    {
        return checkedOut;
    }

    public void ListBooks()
    {
        for (Item e : itemsList)
        {
            if (e.getClass() == Book.class)
            {
                System.out.println(e.getInfo());
            }
        }
    }

    public void ListMovies()
    {
        for (Item item : itemsList)
        {
            if (item.getClass() == Movie.class)
            {
                System.out.println(item.getInfo());
            }
        }
    }

    public void Details(int id)
    {
        for (Item item : itemsList)
        {
            if (item.id == id)
            {
                System.out.println(item.getDetails());

                return;
            }
        }

        System.out.println("Item not found.");
    }

    public void Checkout(int id, User user)
    {
        for (Item item : itemsList)
        {
            if (item.id == id)
            {
                checkedOut.put(item, user);
                itemsList.remove(item);
                System.out.println("Thank you. Enjoy!");

                return;
            }
        }

        System.out.println("Item not found or already checked out.");
    }

    public void Return(int id)
    {
        Iterator it = checkedOut.entrySet().iterator();

        while(it.hasNext())
        {
            Map.Entry pairs = (Map.Entry) it.next();
            Item item = (Item) pairs.getKey();

            if (item.id == id)
            {
                itemsList.add(item);
                it.remove();
                System.out.println("Item returned successfuly. Thank you!");

                return;
            }

        }

        System.out.println("Invalid item for return.");
    }

}
